// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

import Ink from "./Ink";
import PlayerCtrl from "./PlayerCtrl";

const { ccclass, property } = cc._decorator;

@ccclass
export default class test extends cc.Component {
    @property([cc.Prefab])
    inks: cc.Prefab[] = [];

    @property(cc.Node)
    inkLayer: cc.Node = null;

    @property(PlayerCtrl)
    playerCtrl: PlayerCtrl = null;

    private colorH: number = 0;

    protected onLoad(): void {
        cc.director.getPhysicsManager().enabled = true;
    }

    protected onEnable(): void {
        this.playerCtrl.node.on(PlayerCtrl.EventType.GENERATE_INK, this.generateInk, this);
    }

    protected onDisable(): void {
        this.playerCtrl.node.off(PlayerCtrl.EventType.GENERATE_INK, this.generateInk, this);
    }

    protected update(dt: number): void {

    }

    private generateInk(posArr: cc.Vec2[]) {
        for (let v of posArr) {
            let inkPrefab = this.inks[Math.floor(Math.random() * this.inks.length)];
            let ink = cc.instantiate(inkPrefab);
            let inkCom = ink.getComponent(Ink);
            ink.setPosition(this.inkLayer.convertToNodeSpaceAR(v));
            ink.parent = this.inkLayer;
            //颜色
            this.colorH = (this.colorH + 0.01) % 1;
            ink.color = cc.color().fromHSV(this.colorH, 1, 1);
            //角度
            ink.angle = Math.random() * 360;

            //缩放
            ink.scale = Math.random() * (inkCom.maxScale - inkCom.minScale) + inkCom.minScale;
            // ink.scaleY = Math.random() * (inkCom.maxScale - inkCom.minScale) + inkCom.minScale;
        }
    }
}
